package com.cf.r1020;

import java.io.*;

public class d {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer in = new StreamTokenizer(br);
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException {
        in.nextToken(); int T = (int) in.nval;
        while (T-- > 0) {
            solve();
        }
        out.flush();
        out.close();
        br.close();
    }

    static void solve() throws IOException {
        in.nextToken(); int n = (int) in.nval;
        in.nextToken(); int m = (int) in.nval;
        int[] a = new int[n];
        int[] b = new int[m];
        for (int i = 0; i < n; i++) {
            in.nextToken(); a[i] = (int) in.nval;
        }
        for (int i = 0; i < m; i++) {
            in.nextToken(); b[i] = (int) in.nval;
        }
        int[] pre = new int[n+1];
        pre[0] = 0;
        for (int i = 1; i <= n; ++i) {
            pre[i] = pre[i-1];
            if (pre[i-1] < m && a[i-1] >= b[pre[i-1]]) {
                pre[i] = pre[i-1] + 1;
            }
        }
        if (pre[n] >= m) {
            out.println(0);
            return;
        }

        int[] suf = new int[n+1];
        suf[n] = 0;
        for (int i = n-1; i >= 0; --i) {
            suf[i] = suf[i+1];
            if (suf[i+1] < m && a[i] >= b[m - 1 - suf[i+1]]) {
                suf[i] = suf[i+1] + 1;
            }
        }

        long ans = Long.MAX_VALUE;
        for (int pos = 0; pos <= n; ++pos) {
            int leftMatched = pre[pos];
            int rightMatched = suf[pos];
            if (leftMatched + rightMatched >= m) {
                out.println(0);
                return;
            }
            if (leftMatched + 1 + rightMatched >= m) {
                ans = Math.min(ans, (long)b[leftMatched]);
            }
        }
        out.println(ans == Long.MAX_VALUE ? -1 : ans);
    }
}
